package com.yisu.expressway.customService.utils;

import android.view.View;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.application.ExApplication;
import com.yisu.expressway.customService.adapter.VoiceAssistantAdapter;
import com.yisu.expressway.customService.model.ChatContentObj;
import com.yisu.expressway.customService.model.HighWayInfoListObj;
import com.yisu.expressway.customService.model.HighWayInfoObj;
import com.yisu.expressway.customService.model.ServiceModuleID;
import com.yisu.expressway.location.LocationInfo;
import com.yisu.expressway.model.ServiceDistrictItem;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.service_district.DistrictDetailActivity;
import com.yisu.expressway.service_district.SearchDistrictAreaActivity;
import com.yisu.expressway.service_district.ServiceDistrictListActivity;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;
import com.yisu.expressway.weather.model.DailyWeatherObj;
import com.yisu.expressway.weather.model.WeatherObj;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/2 0002.
 */

public class GetAnswerUtils {

    public interface answerResultListener {
        void onGetAnswer(ChatContentObj answerObj);
    }

    public interface NaviRouteResultListener{
        void onGetRouteResult(String start,String end);
    }

    public interface LocationResultListener{
        void onGetLocationResult(double longitude,double latitude);
    }

    public final static int HIGHWAY_INFO_LEVEL_FEE_RULES = 10000001;
    public final static int HIGHWAY_INFO_LEVEL_ILLEGAL = 10000002;
    public final static int HIGHWAY_INFO_LEVEL_FEE_STANDARD = 10000003;
    public final static int HIGHWAY_INFO_LEVEL_RESCUE = 10000004;

    private static PoiSearch mPoiSearch = null;
    private static String[] routePlanKeywordArray = {"到","去","往","至"};

    public static void getWeather(final String city, final answerResultListener listener){
        Map<String, Object> params = new HashMap();
        params.put("location", city);

        ApiClientHelper.postApi(ApiSetting.getWeather(), new TypeToken<WeatherObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<WeatherObj>>() {
            @Override
            public void onResponse(ApiResult<WeatherObj> result) {

                if (!result.success) {
                    listener.onGetAnswer(null);
                    return;
                }

                WeatherObj weatherObj = result.getResult();
                if(weatherObj != null){

                    ArrayList<DailyWeatherObj> dailyList = weatherObj.daily;
                    if(dailyList != null && dailyList.size() > 0){
                        ChatContentObj chatContentObj = new ChatContentObj();
                        chatContentObj.setWeatherObj(weatherObj);

                        DailyWeatherObj todayObj = dailyList.get(0);
                        StringBuilder titleBuilder = new StringBuilder(city);
                        titleBuilder.append("天气").append(": ").append(todayObj.text_day);
                        chatContentObj.setText(titleBuilder.toString());
                        chatContentObj.setWeatherCity(city);

                        StringBuilder tempRange = new StringBuilder(todayObj.low);
                        tempRange.append("/").append(todayObj.high).append("\u2103");

                        chatContentObj.setSecondaryText(tempRange.toString());
                        chatContentObj.setWeatherCode(todayObj.code_day);
                        chatContentObj.setQuestionType(VoiceAssistantAdapter.QUESTION_TYPE_WEATHER);

                        listener.onGetAnswer(chatContentObj);
                    }else {
                        listener.onGetAnswer(null);
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onGetAnswer(null);
            }
        }, "get_weather");
    }

    public static void getHighWayInfo(final int levelId,final String header, final answerResultListener listener){
        Map<String, Object> params = new HashMap();
        params.put("levelId", levelId);

        ApiClientHelper.postApi(ApiSetting.getHighWayInfo(), new TypeToken<HighWayInfoListObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<HighWayInfoListObj>>() {
            @Override
            public void onResponse(ApiResult<HighWayInfoListObj> result) {

                if (!result.success) {
                    listener.onGetAnswer(null);
                    return;
                }

                HighWayInfoListObj listObj = result.getResult();
                if(listObj != null && listObj.voiceAutoQuestionVos != null && !listObj.voiceAutoQuestionVos.isEmpty()){
                    ChatContentObj chatContentObj = new ChatContentObj();
                    ArrayList<HighWayInfoObj> infoObjs = listObj.voiceAutoQuestionVos;
                    HighWayInfoObj firstObj = infoObjs.get(0);


                    boolean isHeader = infoObjs.size() >  1;

                    if(!isHeader){
                        isHeader = firstObj.isHaveSubordinate == 1;
                    }

                    chatContentObj.setHeader(isHeader);

                    if(isHeader){
                        chatContentObj.setQuestionType(VoiceAssistantAdapter.QUESTION_TYPE_RESCUE);
                        chatContentObj.setText(header);
                        chatContentObj.setHighWayInfoList(listObj.voiceAutoQuestionVos);
                    }else {
                        chatContentObj.setText(firstObj.answer);
                        chatContentObj.setQuestionType(VoiceAssistantAdapter.QUESTION_TYPE_IFLY);

                    }

                    listener.onGetAnswer(chatContentObj);
                }else {
                    listener.onGetAnswer(null);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onGetAnswer(null);
            }
        }, "get_highWay_info");

    }

    public static void getNearByDistrictList(LocationInfo locInfo,final answerResultListener listener){
        Map<String, Object> params = new HashMap();
        params.put("longitude", locInfo.getLongitude());
        params.put("latitude", locInfo.getLatitude());
        ApiClientHelper.postApi(ApiSetting.getNearByServiceDistrictList(), new TypeToken<ArrayList<ServiceDistrictItem>>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ArrayList<ServiceDistrictItem>>>() {
            @Override
            public void onResponse(ApiResult<ArrayList<ServiceDistrictItem>> result) {

                if (!result.success) {
                    listener.onGetAnswer(null);
                    return;
                }

                ArrayList<ServiceDistrictItem> mList = result.getResult();
                if(mList == null || mList.size() == 0){
                    listener.onGetAnswer(null);
                    return;
                }

                ChatContentObj chatContentObj = new ChatContentObj();
                chatContentObj.setDistrictItemList(mList);
                chatContentObj.setQuestionType(VoiceAssistantAdapter.QUESTION_TYPE_DISTRICT);
                chatContentObj.setText("您附近的服务区");
                chatContentObj.setHeader(true);
                listener.onGetAnswer(chatContentObj);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onGetAnswer(null);
            }
        }, "get_district_list");
    }


    public static void getWayRoute(String inputContent,NaviRouteResultListener listener){
        String[] resultArray = null;

        for(int i = 0 ;i < routePlanKeywordArray.length ; i++){
            resultArray = inputContent.split(routePlanKeywordArray[i]);
            if(resultArray != null && resultArray.length == 2){
                break;
            }
        }

        if(resultArray != null && resultArray.length == 2){
            listener.onGetRouteResult(resultArray[0],resultArray[1]);
        }else {
            listener.onGetRouteResult(null,null);
        }
    }

    public static void getSearchDistrict(LocationInfo locInfo,final String keyword,final answerResultListener listener){

        Map<String, Object> params = new HashMap();
        params.put("longitude", locInfo.getLongitude());
        params.put("latitude", locInfo.getLatitude());
        params.put("keyword",keyword);

        ApiClientHelper.postApi(ApiSetting.searchDistrict(), new TypeToken<ArrayList<ServiceDistrictItem>>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ArrayList<ServiceDistrictItem>>>() {
            @Override
            public void onResponse(ApiResult<ArrayList<ServiceDistrictItem>> result) {

                if (!result.success) {
                    listener.onGetAnswer(null);
                    return;
                }

                ArrayList<ServiceDistrictItem> mList = result.getResult();
                if(mList == null || mList.size() == 0){
                    listener.onGetAnswer(null);
                    return;
                }

                ServiceDistrictItem item = mList.get(0);

                ChatContentObj chatContentObj = new ChatContentObj();
                chatContentObj.setDistrictItemList(mList);
                chatContentObj.setQuestionType(VoiceAssistantAdapter.QUESTOIN_TYPE_SERRCHE_DISTRICT);
                chatContentObj.setText(item.serviceName);
                chatContentObj.setServiceDistrictItem(item);
                StringBuilder secondaryBuilder = new StringBuilder("距离:");
                secondaryBuilder.append(item.distance).append(" 公里");
                chatContentObj.setSecondaryText(secondaryBuilder.toString());
                listener.onGetAnswer(chatContentObj);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onGetAnswer(null);
            }
        }, "search_district");
    }

    public static void getNeayByWayInfo(LocationInfo currentLocation,final String keyword,final LocationResultListener listener){
        if(mPoiSearch == null){
            mPoiSearch = PoiSearch.newInstance();
        }

        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult result) {
                if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                    listener.onGetLocationResult(-1,-1);
                    return;
                }

                if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                    LatLng location = result.getAllPoi().get(0).location;
                    listener.onGetLocationResult(location.longitude,location.latitude);
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });
        mPoiSearch.searchInCity((new PoiCitySearchOption())
                .city(currentLocation.getCity()).keyword(keyword).pageNum(1));

    }




}
